﻿using Aliyun.OSS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace LeEdu.UI.Controllers
{
	public class TestController : Controller
	{
		//
		// GET: /Test/
		public ActionResult Index()
		{
			return View();
		}

		public ActionResult PluploadQueue()
		{
			return View();
		}

		public ActionResult Uiplupload()
		{
			return View();
		}

		[HttpPost]
		public ActionResult Upload(Guid cid)
		{
			var folderPath=Path.Combine("Uploads/Courses/",cid.ToString());
			FileInfo fileInfo;
			Common.FileUploadHelper.Save(Request,folderPath,out fileInfo);

			/*
			try
			{
				if(Request.Files.Count<=0)
				{
					return null;
				}

				//程序根目录路径
				string appRoot = AppDomain.CurrentDomain.BaseDirectory;
				//下载目录路径
				string uploadDirectory = Path.Combine(appRoot, "Uploads");
				if (!Directory.Exists(uploadDirectory))
				{
					Directory.CreateDirectory(uploadDirectory);
				}
				string fileName = string.Empty;

				int chunk = Convert.ToInt32(Request["chunk"]); //当前分块
				int chunks = Convert.ToInt32(Request["chunks"]);//总的分块数量

				fileName = Request["name"];

				var file = Request.Files[0];
				var saveFilePath = Path.Combine(uploadDirectory, fileName);

				//单块处理
				if (chunks == 1)
				{
					if (null != file && file.ContentLength > 0)
					{
						file.SaveAs(saveFilePath);
					}
				}
				//多块处理
				else
				{
					string tempFilePath = Path.Combine(uploadDirectory,"temp_"+ fileName);
					
					if (chunk == 0)
					{
						//保存第一个分块
						file.SaveAs(tempFilePath);
					}
					else
					{
						if (file.ContentLength > 0)
						{
							//追加分开
							FileStream fs = new FileStream(tempFilePath, FileMode.Append);
							int length = file.ContentLength;
							byte[] input = new byte[length];
							var stream = file.InputStream;
							stream.Read(input, 0, length);
							fs.Write(input, 0, length);
							stream.Close();
							fs.Close();
						}
					}

					//文件合并完处理
					if (chunks - chunk == 1)
					{
						//重命名文件
						FileInfo fi = new FileInfo(tempFilePath);
						fi.MoveTo(saveFilePath);
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			*/
			return null;
		}

		public ActionResult VedioJs()
		{
			return View();
		}

		//阿里云OSS上传示例（Form方式）-适合放在后台管理
		public ActionResult AliyunUploadForm()
		{
			return View();
		}

		public ActionResult AliyunBucketTest(string para="")
		{
			var endPoint = "oss-cn-hangzhou.aliyuncs.com";//杭州
			var accessKeyId = "RoOsC9sfwbifV1tb";
			var accessKeySecret = "GFVoNU9VgQsV3RRQEcBx5hCQkWvERw";
			var bucketName="zhishile-com";

			var ossClient = new OssClient(endPoint, accessKeyId, accessKeySecret);

			//获取Bucket列表
			var buckets = ossClient.ListBuckets();
			StringBuilder sb = new StringBuilder();
			foreach (var item in buckets)
			{
				sb.Append(String.Format("Name:{0} Location:{1} Owner:{2} CreationDate:{3}", item.Name, item.Location, item.Owner, item.CreationDate));
				sb.Append(Environment.NewLine);
				//Name:zhishile Location:oss-cn-beijing Owner:[Owner Id=1339959027174799, DisplayName=1339959027174799] CreationDate:15/11/29 01:00:20
			}

			//获取Object列表

			var listResult= ossClient.ListObjects(bucketName);
			foreach (var summary in listResult.ObjectSummaries)
			{
				sb.Append(String.Format("Key:{0} BucketName:{1} ETag:{2} LastModified:{3} Owner:{4} Size:{5} StorageClass:{6}", summary.Key, summary.BucketName, summary.ETag, summary.LastModified, summary.Owner, summary.Size, summary.StorageClass));
				sb.Append(Environment.NewLine);
				break;
			}
			//Key:0.png BucketName:zhishile-com ETag:6201FB9EEC77CCBCFE3DF8ED1D618B48 LastModified:16/4/11 05:19:47 Owner:[Owner Id=1339959027174799, DisplayName=1339959027174799] Size:2851557 StorageClass:Standard

			//var key="0.png";
			var key = "IMG_0599.MP4";
			//获取Object签名Url
			var uri= ossClient.GeneratePresignedUri(bucketName, key);

			sb.Append(uri.AbsoluteUri);

			return Content(sb.ToString());
		}

	}
}